# 导入pymodbus库
from pymodbus.client import ModbusTcpClient

# 创建Modbus TCP客户端对象
client = ModbusTcpClient('192.168.0.10', port=502)

# 连接Modbus TCP服务器
client.connect()

# 读取保持寄存器的数据
result = client.read_holding_registers(address=0, count=1)

# 处理读取结果
if result.isError():
    print("读取保持寄存器失败：{0}".format(result))
else:
    print("读取保持寄存器成功：{0}".format(result.registers[0]))

# # 写入保持寄存器的数据
# result = client.write_register(address=0, value=123, unit=1)
#
# # 处理写入结果
# if result.isError():
#     print("写入保持寄存器失败：{0}".format(result))
# else:
#     print("写入保持寄存器成功")

# 关闭Modbus TCP客户端连接
client.close()
